import axios from "axios"


type Method = 'get' | 'post' | 'put' | 'delete'
type ResponseData = {
    method: Method
    url: string
    data?: any
    headers?: any
}
const baseUrl = 'http://127.0.0.1:5000'
export const http = <T>(resdata: ResponseData) => {
    return new Promise((resolve, reject) => {
        axios({
            method: resdata.method,
            url: baseUrl + resdata.url,
            params: resdata.data,
            headers: resdata.headers
        }).then(res => {
            if (res.status === 200) {
                resolve(res.data as T)
            }else {
                reject(new Error(`status code is ${res.status}`))
            }
        }).catch(err => {
            console.log('status code is not 200', err)
            reject(err.message)
        })
    })
}
axios.interceptors.request.use(config => {
    config.timeout = 5000
    return config;
}, error => {
    return Promise.reject(error);
})